package com.xzh.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xzh.model.Reviews;
import com.xzh.model.Students;
import com.xzh.model.search.ReviewDao;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface ReviewsMapper extends BaseMapper<Reviews> {

    @Insert("INSERT INTO course_reviews (student_id, course_id, rating, comment, review_time,status) " +
            "VALUES (#{studentId}, #{courseId}, #{rating}, #{comment}, #{reviewTime},#{status})")
    boolean save(ReviewDao reviewDao);

    @Select("SELECT cr.*, c.course_name as courseName, s.student_name as studentName " +
            "FROM course_reviews cr " +
            "LEFT JOIN courses c ON cr.course_id = c.course_id " +
            "LEFT JOIN students s ON cr.student_id = s.student_id")
    List<Reviews> selectListWithNames();

    default List<Map<String, Object>> statistics(){
        QueryWrapper<Reviews> qw = new QueryWrapper<>();
        qw.select("rating", "COUNT(*) as count")
                .groupBy("rating");
        return selectMaps(qw);
    }
}
